package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.CouponProductRelation;
import com.example.mall.domain.CouponReceive;
import com.example.mall.dto.CouponProductRelationDto;
import com.example.mall.dto.CouponReceiveDto;
import com.example.mall.mapper.CouponReceiveMapper;
import com.example.mall.query.CouponProductRelationQuery;
import com.example.mall.query.CouponReceiveQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.ICouponReceiveService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 优惠券领取记录表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-06
 */
@Service
public class CouponReceiveServiceImpl extends ServiceImpl<CouponReceiveMapper, CouponReceive> implements ICouponReceiveService {

    @Autowired
    private CouponReceiveMapper couponReceiveMapper;

    @Override
    public PageDTO<CouponReceiveDto> queryPage(CouponReceiveQuery couponReceiveQuery) {
        if (couponReceiveQuery == null) {
            couponReceiveQuery = new CouponReceiveQuery();
        }
        Page<CouponReceive> page = couponReceiveQuery.toMpPageByDefaultCreateTimeAsc();
        Page<CouponReceive> cartPage = lambdaQuery()
                .like(couponReceiveQuery.getReceiveId()!= null, CouponReceive::getReceiveId, couponReceiveQuery.getReceiveId())
                .like(couponReceiveQuery.getUserId()!= null, CouponReceive::getUserId, couponReceiveQuery.getUserId())
                .like(couponReceiveQuery.getTemplateId()!= null, CouponReceive::getTemplateId, couponReceiveQuery.getTemplateId())
                .like(couponReceiveQuery.getCouponCode()!= null, CouponReceive::getCouponCode, couponReceiveQuery.getCouponCode())
                .apply(couponReceiveQuery.getReceiveTime() != null,
                        "DATE_FORMAT(receive_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        couponReceiveQuery.getReceiveTime())
                .like(couponReceiveQuery.getStatus()!= null, CouponReceive::getStatus, couponReceiveQuery.getStatus())
                .orderByDesc(CouponReceive::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, CouponReceiveDto.class);
    }

    @Override
    public void insert(CouponReceiveDto couponReceiveDto) {
        CouponReceive couponReceive = new CouponReceive();
        CopyUtils.copyProperties(couponReceiveDto, couponReceive);
        couponReceive.setReceiveId(CodeGenerateUtils.generateUniqueId());
        couponReceive.setCreateDate(new Date());
        couponReceive.setUpdateDate(new Date());
        couponReceive.setStatus(BaseConstants.EFFECTIVE_STATUS);
        couponReceiveMapper.insert(couponReceive);
    }

    @Override
    public void update(CouponReceiveDto couponReceiveDto) {
        AssertUtil.isNotNull(couponReceiveDto.getReceiveId(), "receiveId不能为空");
        CouponReceive couponReceive = couponReceiveMapper.selectById(couponReceiveDto.getReceiveId());
        AssertUtil.isNotNull(couponReceive, "优惠券领取记录不存在");
        CopyUtils.copyProperties(couponReceiveDto, couponReceive);
        couponReceive.setUpdateDate(new Date());
        couponReceiveMapper.updateById(couponReceive);
    }

    @Override
    public void delete(Long receiveId) {
        AssertUtil.isNotNull(receiveId, "receiveId不能为空");
        CouponReceive couponReceive = couponReceiveMapper.selectById(receiveId);
        AssertUtil.isNotNull(couponReceive, "优惠券领取记录不存在");
        couponReceive.setStatus(BaseConstants.LOSE_STATUS);
        couponReceive.setUpdateDate(new Date());
        couponReceiveMapper.updateById(couponReceive);
    }

    @Override
    public void deleteBatch(List<Long> receiveIds) {
        AssertUtil.isNotNull(receiveIds, "receiveIds不能为空");
        List<CouponReceive> couponReceives = couponReceiveMapper.selectBatchIds(receiveIds);
        AssertUtil.isNotNull(couponReceives, "优惠券领取记录不存在");
        for (CouponReceive couponReceive : couponReceives) {
            couponReceive.setStatus(BaseConstants.LOSE_STATUS);
            couponReceive.setUpdateDate(new Date());
            couponReceiveMapper.updateById(couponReceive);
        }
    }

    @Override
    public CouponReceive queryCouponReceiveById(Long receiveId) {
        AssertUtil.isNotNull(receiveId, "receiveId不能为空");
        return couponReceiveMapper.selectById(receiveId);
    }

    @Override
    public List<CouponReceive> queryCouponReceiveList() {
        return this.list();
    }
}
